package tw.com.softleader.fire.setting.web;

import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import tw.com.softleader.fire.setting.entity.DeductibleEntity;
import tw.com.softleader.fire.setting.service.DeductibleService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 * @author mdoing2001
 *
 */
public class DeductibleAction extends ActionSupport {

	private Logger log = LoggerFactory.getLogger(DeductibleAction.class);
	
	@Autowired
	private DeductibleService deductibleService;
	
	private List<DeductibleEntity> models;
	private DeductibleEntity model;
	
	public List<DeductibleEntity> getModels() {
		return models;
	}

	public void setModels(List<DeductibleEntity> models) {
		this.models = models;
	}
	
	public DeductibleEntity getModel() {
		return model;
	}

	public void setModel(DeductibleEntity model) {
		this.model = model;
	}

	private String searchBox;
	
	public String getSearchBox() {
		return searchBox;
	}

	public void setSearchBox(String searchBox) {
		this.searchBox = searchBox;
	}

	@Override
	public void validate() {
//		log.debug("validate...");
//		models = deductibleService.getAll();
	}
	
	
	public String update() {
		log.debug("update...");
		
		model.setModifiedTime(LocalDateTime.now());
		try {
			deductibleService.update(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		models = deductibleService.getAll();
		log.debug("Models = {}", models);
		return Action.SUCCESS;
	}
	
	public String delete() {
		log.debug("delete...");
		
		try {
			deductibleService.delete(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		models = deductibleService.getAll();
		log.debug("Models = {}", models);
		return Action.SUCCESS;
	}
	
	public String insert() {
		log.debug("insert...");
		LocalDateTime current = LocalDateTime.now();
		model.setCreator("system");
		model.setCreateTime(current);
		model.setModifier("system");
		model.setModifiedTime(current);
		
		try {
			deductibleService.insert(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		models = deductibleService.getAll();
		log.debug("Models = {}", models);
		return Action.SUCCESS;
	}
	
	public String search() {
		log.debug("search...");
		log.debug("Model = {}", model);
		log.debug("searchBox = {}", this.searchBox);
		
		try{
			model = new DeductibleEntity();
			model.setCode(this.searchBox);
			models = deductibleService.getByCode(model);
			
			log.debug("Models = {}", models);
		}
		catch (Exception e){
			e.printStackTrace();
		}
		
		return Action.SUCCESS;
	}
	
	
	
	@SkipValidation
	public String execute() throws Exception {
		log.debug("execute...");
		
		try {
			models = deductibleService.getAll();

			log.debug("Models = {}",models);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
}
